package com.educate.serviceedu.controller;


import com.educate.commonutils.ResultOne;
import com.educate.servicebase.exceptionhander.EducateException;
import com.educate.serviceedu.client.VodClient;
import com.educate.serviceedu.entity.EduVideo;
import com.educate.serviceedu.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author educate.wht
 * @since 2022-08-16
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private VodClient vodClient;
    //添加小节
    @PostMapping("addVideo")
    public ResultOne addVideo(@RequestBody EduVideo eduVideo) {
        eduVideoService.save(eduVideo);
        return ResultOne.ok();
    }

    //删除小节同时也删除小节视频
    @DeleteMapping("deleteVideo/{id}")
    public ResultOne deleteVideo(@PathVariable String id){
        //根据小节id查视频id
        EduVideo eduVideo = eduVideoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        if(!StringUtils.isEmpty(videoSourceId)){
            //根据视频id,远程调用实现视频删除
            ResultOne resultOne = vodClient.removeAllVideo(videoSourceId);
            if(resultOne.getCode()==20001){
                throw new EducateException(20001,"删除视频失败,熔断器.....");
            }

        }
        eduVideoService.removeById(id);
        return ResultOne.ok();

    }
    //修改小节
    @PostMapping("updateVideo")
    public ResultOne updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return ResultOne.ok();
    }
    //根据小结id查询小结
    @GetMapping("getVideo/{id}")
    public ResultOne getVideo(@PathVariable String id){
        EduVideo eduVideo = eduVideoService.getById(id);
        return ResultOne.ok().data("video",eduVideo);
    }

}

